<template>
  <el-container>
    <el-header>
      <el-row :gutter="24">
        <el-col :span="6">
          <h3>添加数据页面</h3>
        </el-col>
      </el-row>
    </el-header>
    <el-main>
      <div id="新增APP表单" style="padding: 0 20%">
        <el-form ref="infoFrom" :rules="infoAddRules" :model="infoFrom" label-width="100px">
          <el-form-item label="员工名称">
            <el-input v-model="infoFrom.employeeName" placeholder="请输入员工名称"/>
          </el-form-item>
          <el-form-item label="员工学历">
            <el-select v-model="infoFrom.employeeEducation" placeholder="请选择">
              <el-option
                v-for="item in eduOption"
                :key="item.id"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="infoFrom.inductionTime"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="职位">
            <el-select v-model="infoFrom.positionId" placeholder="请选择">
              <el-option
                v-for="item in posData"
                :key="item.positionId"
                :label="item.positionName"
                :value="item.positionId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="infoFrom.employeeSex" placeholder="请选择">
              <el-option
                v-for="item in sexOption"
                :key="item.id"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="infoFrom.employeeStatus" placeholder="请选择">
              <el-option
                v-for="item in statusOption"
                :key="item.id"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('infoFrom')">提交</el-button>
            <el-button type="primary" @click="resetForm('infoFrom')">重置</el-button>
            <el-button type="danger" @click="$router.push('/info/index')">返回首页</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
  </el-container>
</template>

<script>

import moment from 'moment'
import {upd, getOne} from '@/api/info'
import { getPos } from '@/api/pos'

export default {
  name: 'Login',
  data() {
    return {
      infoFrom: {
        employeeId: parseInt(this.$route.query.employeeId),
        employeeName: '',
        employeeEducation: '',
        inductionTime: '',
        positionId: '',
        employeeSex: '',
        employeeStatus: ''
      },
      // 职位数据
      posData: [],
      // 学历
      eduOption: [
        {
          id: 1,
          name: '大专及以下',
          value: 1
        },
        {
          id: 2,
          name: '本科',
          value: 2
        },
        {
          id: 3,
          name: '硕士',
          value: 3
        },
        {
          id: 4,
          name: '博士',
          value: 4
        }
      ],
      // 性别
      sexOption: [
        {
          id: 1,
          name: '男',
          value: '1'
        },
        {
          id: 2,
          name: '女',
          value: '2'
        }
      ],
      // 状态
      statusOption: [
        {
          id: 1,
          name: '在职',
          value: 0
        },
        {
          id: 2,
          name: '离职',
          value: 1
        },
        {
          id: 3,
          name: '请假',
          value: 2
        }
      ]
    }
  },
  watch: {
    $route: {}
  },
  created() {
  },
  mounted() {
    this.init()
  },
  destroyed() {
  },
  methods: {
    init() {
      this.getOne()
      this.getPos()
    },
    // 获取一个
    getOne() {
      getOne({ id: this.infoFrom.employeeId }).then(res => {
        this.infoFrom = res.data
      })
    },
    // 获取全部pos
    getPos() {
      getPos().then(res => {
        this.posData = res.data
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          upd(this.infoFrom).then(res => {
            this.$message({
              message: res.data,
              type: 'success'
            })
          })
          this.$router.push('/info/index')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.infoFrom = {
        employeeName: '',
        employeeEducation: '',
        inductionTime: '',
        positionId: '',
        employeeSex: '',
        employeeStatus: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-header, .el-footer {
  background-color: #B3C0D1;
  color: #333;
  //text-align: center;
  //line-height: 60px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  //text-align: center;
  //line-height: 160px;
}
</style>
